﻿package baidu.reader.common.model
{
    import baidu.lib.utils.*;
    import baidu.reader.common.model.VO.*;
    import flash.display.*;

    public class AppConfigModel extends Object
    {
        private var _loaderInfo:LoaderInfo;
        private var _serialid:String;
        private var _content_url_template:String = "http://wenku.baidu.com/as/play/${0}?${1}";
        private var _toc_url_template:String = "http://wenku.baidu.com/sdir/${0}?lf=0";
        private var _default_rn:int = 50;
        private var isDebug:Boolean = false;
        private var dirIndexXml:XML;
        private var _goLatestChpater:Boolean = false;
        private var _serialTotalPage:int;
        private var cachedPageArray:Array;

        public function AppConfigModel()
        {
            this.cachedPageArray = [];
            return;
        }// end function

        private function parseLoaderInfo(param1:LoaderInfo) : void
        {
            var _loc_2:* = param1.parameters;
            if (!_loc_2)
            {
                return;
            }
            this._serialid = _loc_2["serialid"];
            this._content_url_template = _loc_2["docurl"] + "/${0}?${1}";
            this._toc_url_template = _loc_2["docdirurl"] + "/${0}?lf=0";
            this._default_rn = int(_loc_2["fpn"]);
            this.isDebug = int(_loc_2["debug"]) == 1 ? (true) : (false);
            this._goLatestChpater = int(_loc_2["lastChapter"]) == 1 ? (true) : (false);
            if (this.isDebug)
            {
                this._content_url_template = this._content_url_template + "&debug=1";
                this._toc_url_template = this._toc_url_template + "&debug=1";
            }
            this.dirIndexXml = new XML(_loc_2["dir_index"]);
            this.parseDirXML();
            return;
        }// end function

        private function parseDirXML() : void
        {
            this._serialTotalPage = int(this.dirIndexXml.totalpage);
            return;
        }// end function

        public function getFirstPage(param1:String) : int
        {
            var docid:* = param1;
            if (!this.cachedPageArray[docid])
            {
                var _loc_4:int = 0;
                var _loc_5:* = this.dirIndexXml.docindex.id;
                var _loc_3:* = new XMLList("");
                for each (_loc_6 in _loc_5)
                {
                    
                    var _loc_7:* = _loc_5[_loc_4];
                    with (_loc_5[_loc_4])
                    {
                        if (@key == docid)
                        {
                            _loc_3[_loc_4] = _loc_6;
                        }
                    }
                }
                this.cachedPageArray[docid] = int(_loc_3);
            }
            return this.cachedPageArray[docid];
        }// end function

        public function getPositionBySerialPage(param1:int) : TocItemVO
        {
            var _loc_3:XML = null;
            var _loc_4:int = 0;
            var _loc_5:int = 0;
            var _loc_2:Array = [];
            for each (_loc_3 in this.dirIndexXml.docindex.id)
            {
                
                _loc_2[(int(_loc_3) + 1)] = String(_loc_3.@key);
            }
            _loc_4 = 0;
            _loc_5 = param1;
            while (_loc_5 >= 0)
            {
                
                if (_loc_2[_loc_5])
                {
                    _loc_4 = param1 - _loc_5 + 1;
                    break;
                }
                _loc_5 = _loc_5 - 1;
            }
            return new TocItemVO(_loc_4, 0, "", _loc_2[_loc_5]);
        }// end function

        public function getSerialPageByDocidPage(param1:String, param2:int) : int
        {
            var item:XMLList;
            var result:int;
            var docid:* = param1;
            var pn:* = param2;
            var _loc_5:int = 0;
            var _loc_6:* = this.dirIndexXml.docindex.id;
            var _loc_4:* = new XMLList("");
            for each (_loc_7 in _loc_6)
            {
                
                var _loc_8:* = _loc_6[_loc_5];
                with (_loc_6[_loc_5])
                {
                    if (@key == docid)
                    {
                        _loc_4[_loc_5] = _loc_7;
                    }
                }
            }
            item = _loc_4;
            result = parseInt(item.toString()) + pn;
            return result;
        }// end function

        public function getContentURL(param1:String, param2:String) : String
        {
            return StringTemplate.output(this._content_url_template, param1, param2);
        }// end function

        public function getTocURL() : String
        {
            return StringTemplate.output(this._toc_url_template, this._serialid);
        }// end function

        public function get content_url_template() : String
        {
            return this._content_url_template;
        }// end function

        public function set content_url_template(param1:String) : void
        {
            this._content_url_template = param1;
            return;
        }// end function

        public function get toc_url_template() : String
        {
            return this._toc_url_template;
        }// end function

        public function set toc_url_template(param1:String) : void
        {
            this._toc_url_template = param1;
            return;
        }// end function

        public function get loaderInfo() : LoaderInfo
        {
            return this._loaderInfo;
        }// end function

        public function set loaderInfo(param1:LoaderInfo) : void
        {
            this._loaderInfo = param1;
            this.parseLoaderInfo(this._loaderInfo);
            return;
        }// end function

        public function get serialid() : String
        {
            return this._serialid;
        }// end function

        public function set serialid(param1:String) : void
        {
            this._serialid = param1;
            return;
        }// end function

        public function get default_rn() : int
        {
            return this._default_rn;
        }// end function

        public function set default_rn(param1:int) : void
        {
            this._default_rn = param1;
            return;
        }// end function

        public function get serialTotalPage() : int
        {
            return this._serialTotalPage;
        }// end function

        public function get goLatestChpater() : Boolean
        {
            return this._goLatestChpater;
        }// end function

    }
}
